package com.example.qu_tao.View;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.qu_tao.R;
import com.example.qu_tao.adapter.MyPagerAdapter;
import com.example.qu_tao.database.DBHelper;
import com.google.android.material.tabs.TabLayout;

public class WodeDianpuActivity extends AppCompatActivity {

    private DBHelper mDBHelper;
    private static final int REQUEST_CODE_PUBLISH_CAIGOU = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wodedianpu);

        mDBHelper = new DBHelper(this);

        // 返回按钮点击事件
        ImageButton btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(v -> {
            finish(); // 直接关闭当前Activity
        });
        // 打电话按钮点击事件
        Button btnCall = findViewById(R.id.btn_call);
        btnCall.setOnClickListener(v -> {
            showCallDialog(); // 弹出拨打电话确认弹窗
        });
        // 聊一聊按钮点击事件
        Button btnChat = findViewById(R.id.btn_chat);
        btnChat.setOnClickListener(v -> {
            // 跳转到聊天界面
            Intent intent = new Intent(WodeDianpuActivity.this, LiaoYiLiaoActivity.class);
            startActivity(intent);
        });

        // 获取用户名并显示
        setSellerName();

        // 设置ViewPager和TabLayout
        ViewPager viewPager = findViewById(R.id.view_pager);
        MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        TabLayout tabLayout = findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setSellerName() {
        TextView tvSellerName = findViewById(R.id.tv_seller_name);
        SQLiteDatabase db = mDBHelper.getReadableDatabase();

        // 假设当前登录用户的ID为1，你需要根据实际情况修改
        int currentUserId = 1;
        String[] projection = {"user_name"};
        String selection = "_id = ?";
        String[] selectionArgs = {String.valueOf(currentUserId)};

        Cursor cursor = db.query(
                DBHelper.TABLE_NAME_USER,
                projection,
                selection,
                selectionArgs,
                null,
                null,
                null
        );

        if (cursor.moveToFirst()) {
            @SuppressLint("Range") String userName = cursor.getString(cursor.getColumnIndex("user_name"));
            tvSellerName.setText(userName);
        }
        cursor.close();
    }

    // 显示拨打电话确认弹窗
    private void showCallDialog() {
        new AlertDialog.Builder(this)
                .setTitle("拨打电话")
                .setMessage("确定要拨打电话给商家吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    // 实际拨打电话的逻辑（需要权限处理）
                    Toast.makeText(this, "正在拨打电话...", Toast.LENGTH_SHORT).show();
                    // 这里可以添加实际的拨号代码：
                    // Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:17519246041"));
                    // startActivity(intent);
                })
                .setNegativeButton("取消", null)
                .show();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_PUBLISH_CAIGOU && resultCode == RESULT_OK) {
            if (data.getBooleanExtra("refresh", false)) {
                // 刷新采购信息页面
                // 这里可以通过ViewPager获取CaiGouXinXiFragment并调用其更新方法
            }
        }
    }
}
